<?php
namespace app\index\model;

use categoryclass\Categoryclass;
use think\Model;

class Category extends Model
{
    //关闭时间戳
    protected $autoWriteTimestamp = false;

    /**
     * sort_order排序后的栏目列表
     * @return mixed
     */
    public function cate_list()
    {
        return $this->order('sort_order desc')->select();
    }

    /**
     * 显示已经排序的栏目列表
     * @return array
     */
    public function sort_list()
    {
        $list = $this->cate_list(); //所有的栏目
        return Categoryclass::catesort($list);
    }

    /**
     * 首页导航显示
     * @return array
     */
    public function index_list()
    {
        $list = $this->cate_list();
        return Categoryclass::orm_catesorts($list);
    }

    //首页右侧两导航
    public function right_nav()
    {
        $list = $this->cate_list();
        //1.先循环一遍找出id为 1和3的父栏目
        foreach ($list as $v) {
            if ($v->id == 1) $arr[0][] = $v;
            if ($v->id == 3) $arr[1][] = $v;
        }

        //2.再次循环数据，取出子栏目
        foreach ($list as $v) {
            if ($v->parent_id == 1) $arr[0][] = $v;
            if ($v->parent_id == 3) $arr[1][] = $v;
        }
        return $arr;
    }

    /**
     * 模型数据验证
     * @param $data
     * @return bool|string
     */
    public function data_verify($data)
    {
        //$Category = new Category();
        $update = isset($data['id']) ? ['id' => $data['id']] : []; //是否更新操作
        $result = $this->validate(
            [
                'parent_id' => 'require|number',
                'name' => 'require|max:30',
            ],
            [
                'parent_id.require' => ' 父级栏目不能为空 ',
                'parent_id.number' => ' 父级栏目必须是数字 ',
                'name.require' => ' 栏目名称不能为空 ',
                'name.max' => ' 栏目名称不能超过30个字符 ',
            ],
            [
                'add' => ['parent_id','name'],
            ]
        )->allowField(true)->save($data, $update);
        if(false === $result){
            //  验证失败 输出错误信息
            $error = $this->getError();
            if (is_array($error)) {
                return $error[key($error)];
            }else {
                return $error;
            }
        }
        return true;
    }

    /**
     * 分页
     */
    public function pageQuery(){
        $key = input('get.key');
        $where = [];
        $where['a.dataFlag'] = 1;
        if($key!='')$where['a.articleTitle'] = ['like','%'.$key.'%'];
        $page = Db::name('articles')->alias('a')
            ->join('__ARTICLE_CATS__ ac','a.catId= ac.catId','left')
            ->join('__STAFFS__ s','a.staffId= s.staffId','left')
            ->where($where)
            ->field('a.id,a.catId,a.articleTitle,a.isShow,a.articleContent,a.articleKey,a.createTime,ac.catName,s.staffName')
            ->order('a.id', 'desc')
            ->paginate(input('post.pagesize/d'))->toArray();
        if(count($page['Rows'])>0){
            foreach ($page['Rows'] as $key => $v){
                $page['Rows'][$key]['articleContent'] = strip_tags(htmlspecialchars_decode($v['articleContent']));
            }
        }
        return $page;
    }

    /**
     * 获取指定对象
     */
    public function getById($id){
        $single = $this->where(['id'=>$id,'dataFlag'=>1])->find();
        $singlec = Db::name('article_cats')->where(['catId'=>$single['catId'],'dataFlag'=>1])->field('catName')->find();
        $single['catName']=$singlec['catName'];
        $single['articleContent'] = htmlspecialchars_decode($single['articleContent']);
        return $single;
    }

    /**
     * 删除
     */
    public function del($id){
        if (!$id || !is_int($id)) return false;
        $result = $this->get($id);
        //$result = Category::get($id);
        if(false !== $result){
            if ( $result->delete() ) {
                return true;
            }else {
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 根据url 查询栏目id，并且返回面包屑导航
     * @param $url
     * @return array|false|\PDOStatement|string|Model
     */
    public function url_map($url)
    {
        //1.查询栏目id
        $cate_info = $this->where('fore_url', $url)->find();
        if (is_object($cate_info))
            $cate_id  = $cate_info->id;
        else
            $cate_id = $cate_info['id'];
        if (empty($cate_id)) return false;
        //2.查询所有的栏目
        $list = $this->cate_list();
        return Categoryclass::orm_getParents($list, $cate_id);
    }
}